<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:应用权限组
 * @LastEditors: liuhongbin
 */

namespace app\common\model\addonAuth;


use app\common\model\BaseModel;
use app\common\services\facade\Addons;

class AddonGroup extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'addon_group';

    /**
     * 字段类型或者格式转换
     * @var array
     */
    protected $type = [
        'addons' => 'json',
    ];

    /**
     * 获取扩展信息
     * @param $value
     * @param $data
     * @return array
     */
    public function getExplainAttr($value, $data) {
        $addons = json_decode($data['addons'], true);
        // 判断应用是否存在
        $addonNames = array_column((new \app\common\model\addon\Addons())->select()->toArray(), 'name');
        $validAddons = array_filter($addons, function ($key) use ($addonNames) {
            return in_array($key, $addonNames);
        }, ARRAY_FILTER_USE_KEY);
        return $this->getExplainByAddons($validAddons);
    }

    /**
     * 获取扩展列表内容
     * @param $addon_group_addons
     * @return array
     */
    public function getExplainByAddons($addon_group_addons)
    {
        $addonsModel = new \app\common\model\addon\Addons();
        $addons_list = [];
        foreach ($addon_group_addons??[] as $addon_name => $auth_list) {
            $auth_key_title = Addons::getAuthKeyNameByAuth(get_addons_config($addon_name)['auth']??[]); // 获取一维数组
            foreach ($auth_key_title as $auth_name => $title) {
                if (in_array('all', $auth_list)) {
                    $addons_list['auth'][$addon_name]['all'] = array(
                        'title' => "所有权限",
                        'check' => 1
                    );
                } else {
                    if (in_array($auth_name, $auth_list)) {
                        $addons_list['auth'][$addon_name][$auth_name] = array(
                            'title' => $auth_key_title[$auth_name],
                            'check' => 1
                        );
                    } else {
                        $addons_list['auth'][$addon_name][$auth_name] = array(
                            'title' => $auth_key_title[$auth_name],
                            'check' => 0
                        );
                    }
                }
            }
            $addons_list['addon_msg'][$addon_name] = $addonsModel->where(['name' => $addon_name])->find();
        }
        return $addons_list;
    }

    /**
     * 获取应用组下拉菜单数据
     * @return array[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAddonGroupOption()
    {
        $row = self::select()->toArray();
        $option = [
            [
                'value' => 0,
                'label' => "默认应用组",
            ]
        ];
        foreach ($row as $key => $value) {
            $option[] = [
                'value' => $value['id'],
                'label' => $value['name']
            ];
        }
        return $option;
    }
}
